#!/bin/bash
set -e

../verify-docker-versions

# Detect docker-compose command
DOCKER_COMPOSE_COMMAND=$(docker compose version &>/dev/null && echo 'docker compose' || echo 'docker-compose')
# Pass this value on to the GUI container as an env variable
export INTEGRATION_VERSION=$(git describe --tags --abbrev=0)
# Parse the Mender-Artifact version used from the other-components.yml file's image tag
export MENDER_ARTIFACT_VERSION=$(awk -F':' '/mendersoftware\/mender-artifact/ {print $3}' ../other-components.yml)
# Parse the mender version from docker-compose.yml mender image's tag
export MENDER_VERSION=$(awk -F':' '/mendersoftware\/mender-client/ {print $3}' ../docker-compose.client.yml)
export MENDER_DEB_PACKAGE_VERSION=$MENDER_VERSION

if [[ "$OSTYPE" == "darwin"* ]]; then
    export GATEWAY_IP=$(ifconfig | sed -En 's/127.0.0.1//;s/.*inet (addr:)?(([0-9]*\.){3}[0-9]*).*/\2/p' | head -1)
else
    export GATEWAY_IP=$(ip route get 1 | awk '{print $7;exit}')
fi

ENTERPRISE_DOCKER_COMPOSE=
ENTERPRISE_PROD=
if [ -f ./config/enterprise.yml ]; then
    ENTERPRISE_DOCKER_COMPOSE="-f ../docker-compose.enterprise.yml"
    ENTERPRISE_PROD="-f ./config/enterprise.yml"
fi

exec ${DOCKER_COMPOSE_COMMAND} \
     -p menderproduction \
     -f ../docker-compose.yml \
     -f ../docker-compose.storage.minio.yml \
     $ENTERPRISE_DOCKER_COMPOSE \
     -f ./config/prod.yml \
     $ENTERPRISE_PROD \
     "$@"
